﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using School.Entities;
using School.Common;
using School.Admission;
using School.Organization;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
namespace School.Pages.Organization
{
    public partial class privilege : System.Web.UI.Page
    {
        int[] numbers = new int[87];
       
        public static SchoolEntities db = new SchoolEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1305) == true)
                {
                    if (!IsPostBack)
                    {
                        var query = fn_privilage.GetUserInfo();
                        dd_username.DataSource = query;
                       dd_username.DataValueField = "id";
                       dd_username.DataTextField = "name";
                        
                        dd_username.DataBind();
                        gridviewfill();
                        dd_username.Items.Insert(0, new ListItem() { Value = "1", Text = "الكل" });


                    }
                   
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        private void gridviewfill()
        {
            var query2 = fn_privilage.GetAllPrivilege();
            gv_privilege.DataSource =  query2;
            gv_privilege.Columns[1].Visible= true;
            gv_privilege.DataBind();
            
            ;

        }

        //protected void gv_privilege_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{

        //    foreach (GridViewRow r in gv_privilege.Rows)
        //    {
        //        int i = 0;
        //        numbers = (int[])ViewState["page"];
        //        int pagecode = int.Parse(((Label)(r.FindControl("Label1"))).Text);
        //        CheckBox cb = (CheckBox)r.FindControl("CheckBox2");
        //        if (cb.Checked)
        //        {
        //            numbers[i] = pagecode;
        //            i++;

        //        }
               
        //    }

        //    ViewState["paing"] = numbers;
            
        //    gv_privilege.PageIndex = e.NewPageIndex;
        //    gridviewfill();
        //    foreach (GridViewRow r in gv_privilege.Rows)
        //    {

        //  int pagecode = int.Parse(((Label)(r.FindControl("Label1"))).Text);
        //        CheckBox cb = (CheckBox)r.FindControl("CheckBox2");
        //        if (numbers != null)
        //        {
        //            if (numbers.Contains(pagecode))
        //            {
        //                cb.Checked = true;

        //            }
        //        }

        //    }
      
        //}

        protected void dd_username_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow r in gv_privilege.Rows)
            {
                
                CheckBox ck = (CheckBox)r.FindControl("CheckBox2");

               
                    ck.Checked = false;
            }
            if (dd_username.SelectedIndex == 0)
            {
                gridviewfill();
            }

            var pagecode = fn_privilage.getpagecodeID(int.Parse(dd_username.SelectedValue));

            if (pagecode.Count() > 0)
            {
                foreach (GridViewRow r in gv_privilege.Rows)
                {
                    int id = int.Parse(((Label)r.FindControl("Label1")).Text);
                    CheckBox ck = (CheckBox)r.FindControl("CheckBox2");

                    if (pagecode.Contains(id) == true)
                        ck.Checked = true;
                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (dd_username.SelectedIndex != 0)
            {
                 long x  = long.Parse( dd_username.SelectedValue);
                
                var delete = from d in db.SEC_UesersPrivilages
                             where d.UserId == x
                             select d;
                if (delete.Count() > 0)
                {

                    foreach (var r in delete)
                    {
                        db.DeleteObject(r);
                      
                  
                    }
                    db.SaveChanges();
                }

              

               foreach(GridViewRow r in gv_privilege.Rows )
                {
                    int codeid = int.Parse( ((Label)(r.FindControl("Label1"))).Text);
                    CheckBox cb = (CheckBox)r.FindControl("CheckBox2");
                    if (cb.Checked)
                    {                        
                        SEC_UesersPrivilages user = new SEC_UesersPrivilages() { UserId = long.Parse(dd_username.SelectedValue), ScreenCodeId = codeid };
                        db.AddToSEC_UesersPrivilages(user);
                        db.SaveChanges();
                    }
                }

            }




        }
    }
}